<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\ChangeImg;
use app\common\model\ThirdOrder;
use app\common\model\UserQq;
use fast\Random;
use think\Config;
use think\Exception;
use think\Validate;

/**
 * 奖品兑换
 */
class Dchart extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function type(){

        $data = ChangeImg::TypeApi();

        $this->success(__('Get success'),$data);
    }

    /**
     * 福利兑图提交
     */
    public function create(){
        $type = $this->request->post('type');
        $jiepin = $this->request->post('jiepin');
        $xiu_pic = $this->request->post('xiu_pic');
        $remark = $this->request->post('remark');

        $ftype = ChangeImg::fType();
        if(empty($ftype[$type])){
            $this->error(__('Type is error'));
        }

        if($type == 101){
            if(!$jiepin){
                $this->error(__('Screen is error'));
            }
        }else{
            if(!$xiu_pic){
                $this->error(__('Xiu is error'));
            }
        }
        if(!$remark){
            $this->error(__('Remark is error'));
        }

        $count = ChangeImg::where(['user_id'=>$this->auth->id,'type_id'=>$type])->count();
        if($count){
            $this->error(__('Type dchart is use'));
        }

        $data = [
            'user_id'       => $this->auth->id,
            'type_id'       => $type,
            'jiepin'        => $jiepin,
            'remark'        => $remark,
            'xiu_pic'       => $xiu_pic,
        ];
        ChangeImg::create($data);
        $this->error(__('Operation completed'));
    }

    /**
     * 兑图记录
     */
    public function info(){
        $type = $this->request->get('type');
        if(!$type){
            $this->error(__('Invalid parameters'));
        }

        $info = ChangeImg::where(['user_id'=>$this->auth->id,'type_id'=>$type])->with(['product'=>function($q){
            $q->field('id,title,thump');
        }])->find();
        $this->success(__('Get success'),$info);
    }

}
